package com.sskj.functions.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.functions.R;
import com.sskj.functions.R2;
import com.sskj.functions.presenter.InvestPresenter;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.StrategyBean;
import com.sskj.lib.bean.UsableBean;
import com.sskj.lib.util.ArithUtil;
import com.sskj.lib.util.CommonUtil;
import com.sskj.lib.widget.BottomDialog;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/07/05
 */
@Route(path = RConfig.FUNCTIONS_INVEST)
public class InvestActivity extends BaseActivity<InvestPresenter> {


    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.tvRight)
    TextView tvRight;
    @BindView(R2.id.tvName)
    TextView tvName;
    @BindView(R2.id.tvKetou)
    TextView tvKetou;
    @BindView(R2.id.tvNumber)
    EditText tvNumber;
    @BindView(R2.id.tvType)
    TextView tvType;
    @BindView(R2.id.tvYue)
    TextView tvYue;
    @BindView(R2.id.ivJian)
    ImageView ivJian;
    @BindView(R2.id.tvBeishu)
    TextView tvBeishu;
    @BindView(R2.id.tvJia)
    ImageView tvJia;
    @BindView(R2.id.tvJine)
    TextView tvJine;
    @BindView(R2.id.tvShouyi)
    TextView tvShouyi;
    @BindView(R2.id.cbTrue)
    CheckBox cbTrue;
    @BindView(R2.id.register_ok)
    TextView registerOk;
    @BindView(R2.id.tvXieyi)
    TextView tvXieyi;
    @BindView(R2.id.btStart)
    Button btStart;
    @Autowired
    public int position;
    @Autowired
    public StrategyBean.List1Bean List1Bean;
    private String pay_type = "usdt";
    private int count = 1;
    private String Id;
    private String jishu;

    private String coinName = "";
    private String inMoney;//投资金额
    private String languages = "1";

    @Override
    public int getLayoutId() {
        return R.layout.functions_activity_invest;
    }

    @Override
    public InvestPresenter getPresenter() {
        return new InvestPresenter();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        updateUI(List1Bean);
        tvTitle.setText(App.INSTANCE.getString(R.string.functions_investActivity1));
        tvBeishu.setText(String.valueOf(count));

    }

    private void updateUI(StrategyBean.List1Bean list1Bean) {
        mPresenter.getUsable(list1Bean.getId());
        coinName = list1Bean.getPay_type().toUpperCase();
        tvName.setText(list1Bean.getName());
        tvNumber.setText(NumberUtil.keepNoZero(list1Bean.getShare_min()));
        Id = list1Bean.getId();
        tvKetou.setText(CommonUtil.dealCoinNum(list1Bean.getSurplus(), list1Bean.getPay_type().toUpperCase()) + list1Bean.getPay_type().toUpperCase());
        tvType.setText(list1Bean.getPay_type().toUpperCase());
        String yearIncome = ArithUtil.txfloat(Double.parseDouble(list1Bean.getInterest_rate()), 100);
        try {
            double dayIncome = ArithUtil.div(Double.parseDouble(yearIncome), 365, 8);
            jishu = String.valueOf(ArithUtil.mul(dayIncome, Double.valueOf(list1Bean.getCycle())));
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        updateShouyi();
    }

    @Override
    public void initData() {
        /*点击投资*/
        ClickUtil.click(300, btStart, () -> {
            if (isEmpty(inMoney)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.functions_investActivity2));
                return;
            }
//            if (!cbTrue.isChecked()) {
//                ToastUtil.showShort(App.INSTANCE.getString(R.string.functions_investActivity33));
//                return;
//            }
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(BaseHttpConfig.HETONG);
            stringBuilder.append("?a=");
            stringBuilder.append(SPUtil.get(SPConfig.ACCOUNT, ""));
            stringBuilder.append("&t=");
            stringBuilder.append(SPUtil.get(SPConfig.TOKEN, ""));
            stringBuilder.append("&i=");
            stringBuilder.append(Id);
            stringBuilder.append("&l=");
            stringBuilder.append("cn");
            stringBuilder.append("&c=");
            stringBuilder.append(inMoney);

            ARouter.getInstance().build(RConfig.APP_ACTIVITY_ZHUANPAN)
                    .withString(Constans.TITLE, App.INSTANCE.getString(R.string.functions_functionsHetong))
                    .withString(Constans.WEB_URL, stringBuilder.toString())
                    .navigation(this, Constans.HETONG);

        });
        //协议
        ClickUtil.click(300, tvXieyi, () -> {
            ARouter.getInstance().build(RConfig.FUNCTIONS_AGREEMENT).navigation();
        });
        /*加号*/
        ClickUtil.click(300, ivJian, () -> {
            if (count == 1) {
                return;
            }
            count--;
            tvBeishu.setText(String.valueOf(count));
            updateShouyi();
        });
        /*减号*/
        ClickUtil.click(300, tvJia, () -> {
            count++;
            tvBeishu.setText(String.valueOf(count));
            updateShouyi();
        });
    }

    public void InvestSuccess() {
        ToastUtil.showShort(App.INSTANCE.getString(R.string.functions_investActivity4));
        finish();
    }

    private void updateShouyi() {
        if (isEmpty(getText(tvNumber))) {
            inMoney = "";
            tvShouyi.setText("0 " + coinName);
            tvJine.setText("0 " + coinName);
            return;
        }
        inMoney = CommonUtil.dealCoinNum(String.valueOf(ArithUtil.mul(Double.valueOf(getText(tvNumber)), (double) count)), coinName);
        tvJine.setText(inMoney + coinName);
        if (isEmpty(jishu)) {
            return;
        }
        tvShouyi.setText(
                CommonUtil.dealCoinNum
                        (NumberUtil.keepNoZero
                                        (ArithUtil.mul(Double.parseDouble(getText(tvNumber)) * count, Double.valueOf(jishu)))
                                , coinName) + coinName);

    }

    public void getUsableOK(UsableBean data) {
        if (data == null) {
            return;
        }
        tvYue.setText(App.INSTANCE.getString(R.string.functions_investActivity6) + CommonUtil.dealCoinNum(data.getPrice(), data.getType().toUpperCase()) + data.getType().toUpperCase());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constans.HETONG && resultCode == RESULT_OK) {
            BottomDialog.posswordInput(this, tv -> {
                mPresenter.startInvest(inMoney, Id, tv);
            });
        }
    }
}
